/**
 *\brief socket封装
 */
#ifndef _Socket_H
#define _Socket_H

#include <vector>
#include <netinet/in.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <fcntl.h> 
#include <iostream> 
#include <signal.h> 
#include <sys/epoll.h> 

const unsigned int BUFFERMAXSIZE = 16*1024;
const unsigned int MAXUSERDATASIZE = 1*1024;

template <typename BufferType>
struct ByteBuffer
{
	public:
		ByteBuffer(const unsigned int max):curPtr(0),offPtr(0),maxSize(BUFFERMAXSIZE);
		void reset()
		{
			buff.swap(BufferType());
			curPtr = offPtr = 0;
		}
		void put(const unsigned char *pstr,const unsigned int size)
		{
			if(curPtr+size > maxSize)
			{
				maxSize += BUFFERMAXSIZE/2; 
			}
			curPtr += size;
		}
		inline bool readReady()
		{
			return curPos > offPtr;
		}
		inline unsigned char *wr_buf()
		{
			return &buff[offPtr];
		}
		inline unsigned char *rd_buf()
		{
			return &buff[curPtr];
		}
		inline unsigned int wr_size() const
		{
			return curPtr-offPtr;
		}
		void rd_flap(unsigned int size)
		{
			offPtr += size;
			if(curPtr<2*offPtr)//curPtr-offPtr<offPtr
			{
				memmove(&buff[0],&buff[curPtr],curPtr-offPtr);	
				offPtr = 0;
				curPtr -= offPtr;
			}
		}
	private:
		BufferType buff;
		unsigned int curPtr;
		unsigned int offPtr;
		unsigned int maxSize;
};

typedef ByteBuff<std::vector<unsigned char> > DynBuffer;

class Socket
{
	public:
		recvToBuff(unsigned char *buff,);
};

#endif
